package structure

import (
	"gitcode.com/G-YT/ANN/server/global"
	"gitcode.com/G-YT/ANN/server/model/common/request"
	"gitcode.com/G-YT/ANN/server/model/common/response"
	"gitcode.com/G-YT/ANN/server/model/structure"
	structureReq "gitcode.com/G-YT/ANN/server/model/structure/request"
	"gitcode.com/G-YT/ANN/server/service"
	"gitcode.com/G-YT/ANN/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type ServiceStationArticleApi struct {
}

var serviceStationArticleService = service.ServiceGroupApp.StructureServiceGroup.ServiceStationArticleService

// CreateServiceStationArticle 創建服务站文章
// @Tags ServiceStationArticle
// @Summary 創建服务站文章
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body structure.ServiceStationArticle true "創建服务站文章"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"創建成功"}"
// @Router /serviceStationArticle/createServiceStationArticle [post]
func (serviceStationArticleApi *ServiceStationArticleApi) CreateServiceStationArticle(c *gin.Context) {
	var serviceStationArticle structure.ServiceStationArticle
	err := c.ShouldBindJSON(&serviceStationArticle)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	verify := utils.Rules{
		"ScId":    {utils.NotEmpty()},
		"Title":   {utils.NotEmpty()},
		"Cover":   {utils.NotEmpty()},
		"Details": {utils.NotEmpty()},
	}
	if err := utils.Verify(serviceStationArticle, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	serviceStationArticle.ScId = utils.GetUserID(c)
	if err := serviceStationArticleService.CreateServiceStationArticle(&serviceStationArticle); err != nil {
		global.GVA_LOG.Error("創建失敗!", zap.Error(err))
		response.FailWithMessage("創建失敗", c)
	} else {
		response.OkWithMessage("創建成功", c)
	}
}

// DeleteServiceStationArticle 删除服务站文章
// @Tags ServiceStationArticle
// @Summary 删除服务站文章
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body structure.ServiceStationArticle true "删除服务站文章"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /serviceStationArticle/deleteServiceStationArticle [delete]
func (serviceStationArticleApi *ServiceStationArticleApi) DeleteServiceStationArticle(c *gin.Context) {
	var serviceStationArticle structure.ServiceStationArticle
	err := c.ShouldBindJSON(&serviceStationArticle)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := serviceStationArticleService.DeleteServiceStationArticle(serviceStationArticle); err != nil {
		global.GVA_LOG.Error("删除失敗!", zap.Error(err))
		response.FailWithMessage("删除失敗", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteServiceStationArticleByIds 批量删除服务站文章
// @Tags ServiceStationArticle
// @Summary 批量删除服务站文章
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除服务站文章"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /serviceStationArticle/deleteServiceStationArticleByIds [delete]
func (serviceStationArticleApi *ServiceStationArticleApi) DeleteServiceStationArticleByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := serviceStationArticleService.DeleteServiceStationArticleByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失敗!", zap.Error(err))
		response.FailWithMessage("批量删除失敗", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateServiceStationArticle 更新服务站文章
// @Tags ServiceStationArticle
// @Summary 更新服务站文章
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body structure.ServiceStationArticle true "更新服务站文章"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /serviceStationArticle/updateServiceStationArticle [put]
func (serviceStationArticleApi *ServiceStationArticleApi) UpdateServiceStationArticle(c *gin.Context) {
	var serviceStationArticle structure.ServiceStationArticle
	err := c.ShouldBindJSON(&serviceStationArticle)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := serviceStationArticleService.UpdateServiceStationArticle(serviceStationArticle); err != nil {
		global.GVA_LOG.Error("更新失敗!", zap.Error(err))
		response.FailWithMessage("更新失敗", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindServiceStationArticle 用id査詢服务站文章
// @Tags ServiceStationArticle
// @Summary 用id査詢服务站文章
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query structure.ServiceStationArticle true "用id査詢服务站文章"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"査詢成功"}"
// @Router /serviceStationArticle/findServiceStationArticle [get]
func (serviceStationArticleApi *ServiceStationArticleApi) FindServiceStationArticle(c *gin.Context) {
	var serviceStationArticle structure.ServiceStationArticle
	err := c.ShouldBindQuery(&serviceStationArticle)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if reserviceStationArticle, err := serviceStationArticleService.GetServiceStationArticle(serviceStationArticle.ID); err != nil {
		global.GVA_LOG.Error("査詢失敗!", zap.Error(err))
		response.FailWithMessage("査詢失敗", c)
	} else {
		response.OkWithData(gin.H{"reserviceStationArticle": reserviceStationArticle}, c)
	}
}

// GetServiceStationArticleList 分页獲取服务站文章列表
// @Tags ServiceStationArticle
// @Summary 分页獲取服务站文章列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query structureReq.ServiceStationArticleSearch true "分页獲取服务站文章列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"獲取成功"}"
// @Router /serviceStationArticle/getServiceStationArticleList [get]
func (serviceStationArticleApi *ServiceStationArticleApi) GetServiceStationArticleList(c *gin.Context) {
	var pageInfo structureReq.ServiceStationArticleSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	id := utils.GetUserID(c)
	pageInfo.ScId = id
	if list, total, err := serviceStationArticleService.GetServiceStationArticleInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage("獲取失敗", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "獲取成功", c)
	}
}

// GetArticleById 根据ID獲取服务站文章
// @Tags ServiceStationArticle
// @Summary 根据ID獲取服务站文章
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query structure.ServiceStationArticle true "根据ID獲取服务站文章"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"査詢成功"}"
// @Router /serviceStationArticle/findServiceStationArticle [get]
func (serviceStationArticleApi *ServiceStationArticleApi) GetArticleById(c *gin.Context) {
	var serviceStationArticle structure.ServiceStationArticle
	userID := utils.GetUserID(c)
	err := c.ShouldBindQuery(&serviceStationArticle)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if reserviceStationArticle, err := serviceStationArticleService.GetArticleById(serviceStationArticle.ID, userID); err != nil {
		global.GVA_LOG.Error("査詢失敗!", zap.Error(err))
		response.FailWithMessage("査詢失敗", c)
	} else {
		response.OkWithData(gin.H{"reserviceStationArticle": reserviceStationArticle}, c)
	}
}

// GetArticleList 獲取服务站文章列表
// @Tags ServiceStationArticle
// @Summary 獲取服务站文章列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query structure.ServiceStationArticle true "獲取服务站文章列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"査詢成功"}"
// @Router /serviceStationArticle/findServiceStationArticle [get]
func (serviceStationArticleApi *ServiceStationArticleApi) GetArticleList(c *gin.Context) {
	var pageInfo structureReq.ServiceStationArticleSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := serviceStationArticleService.GetArticleList(pageInfo); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage("獲取失敗", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "獲取成功", c)
	}
}

// GetReadList 獲取文章阅读名单
// @Tags ServiceStationArticle
// @Summary 獲取文章阅读名单
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query structure.ServiceStationArticle true "獲取文章阅读名单"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"査詢成功"}"
// @Router /serviceStationArticle/findServiceStationArticle [get]
func (serviceStationArticleApi *ServiceStationArticleApi) GetReadList(c *gin.Context) {
	var serviceStationArticle structure.ServiceStationArticle
	err := c.ShouldBindQuery(&serviceStationArticle)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if readList, err := serviceStationArticleService.GetReadList(serviceStationArticle.ID); err != nil {
		global.GVA_LOG.Error("査詢失敗!", zap.Error(err))
		response.FailWithMessage("査詢失敗", c)
	} else {
		response.OkWithData(gin.H{"readList": readList}, c)
	}
}
